package com.wanpinghui.supplier.widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.util.AttributeSet;

import com.wanpinghui.supplier.R;

/***
 * 功能描述:
 * 作者:qiujialiu
 * 时间:2017/11/21
 ***/

public class IconFontTextView extends android.support.v7.widget.AppCompatTextView {
    private String normalStr;
    private String selectedStr;

    public IconFontTextView(Context context) {
        this(context, null);
    }

    public IconFontTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public IconFontTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = null;
        if (attrs != null) {
            typedArray = getResources().obtainAttributes(attrs,
                    R.styleable.IconFontTextView);
            if (typedArray != null) {
                normalStr = typedArray.getString(R.styleable.IconFontTextView_normalString);
                selectedStr = typedArray.getString(R.styleable.IconFontTextView_selectedString);
            }
        }
        init();
    }

    private void init() {
        Typeface iconfont = Typeface.createFromAsset(getContext().getAssets(), "iconfont.ttf");
        setTypeface(iconfont);
        if (normalStr != null && normalStr.length() > 0) {
            setText(normalStr);
        }
    }

    @Override
    public void setSelected(boolean selected) {
        super.setSelected(selected);
        if (selected) {
            if (selectedStr != null && selectedStr.length() > 0) {
                setText(selectedStr);
            }
        } else {
            if (normalStr != null && normalStr.length() > 0) {
                setText(normalStr);
            }
        }
    }
}
